﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEB.DAL.Models;
using WEB.MVClient.Models;
using WEB.MVClient.Code;
using System.Data.EntityClient;


namespace WEB.MVClient.Controllers
{
    public class CandidateController : Controller
    {
        private EntityConnection entityConnection = null;

        private const int ItemsOnPage = 20;

        public CandidateController()
        {
            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();

            ecsb.Name = "WebsiteEntities";

            entityConnection = new EntityConnection(ecsb.ConnectionString);
        }

        public ActionResult GetCandidates(int key, int page)
        {
            using (WebSiteEnteties websiteEntities = new WebSiteEnteties(entityConnection))
            {
                CandidateListModel candidateListModels = null;

                #region Get Data
                if (key == 1)
                {
                    ViewBag.Title = "Торгові представники. Менеджери з продажу. Мерчендайзери";
                     candidateListModels =
                          CandidateConverter.Convert(from n in websiteEntities.Candidates
                                                     where n.Candidate_Positions.Any(m => m.PositionID == 2 || m.PositionID == 3 || m.PositionID == 9)
                                                     select n);
                }
                else
                    if (key == 2)
                    {
                        ViewBag.Title = "Супервайзери. Керівники торгового відділу";
                        candidateListModels =
                             CandidateConverter.Convert(from n in websiteEntities.Candidates
                                                        where n.Candidate_Positions.Any(m => m.PositionID == 4)
                                                        select n);
                    }
                    else
                        if (key == 3)
                        {
                            ViewBag.Title = "Територіальні, регіональні менеджери. Комерційні директори. Директори філій";
                            candidateListModels =
                                 CandidateConverter.Convert(from n in websiteEntities.Candidates
                                                            where n.Candidate_Positions.Any(m => m.PositionID == 5 || m.PositionID == 17 || m.PositionID == 19 || m.PositionID == 20)
                                                            select n);
                        }
                #endregion

                #region Pagging

                ViewBag.PageCount = candidateListModels.Candidates.Count() % ItemsOnPage == 0 ? candidateListModels.Candidates.Count() / ItemsOnPage : (int)(candidateListModels.Candidates.Count() / ItemsOnPage) + 1;

                if (candidateListModels.Candidates.ToList().Count > 0)
                {
                    int count = candidateListModels.Candidates.ToList().Count - (page - 1) * ItemsOnPage >= ItemsOnPage ? ItemsOnPage : candidateListModels.Candidates.ToList().Count - (page - 1) * ItemsOnPage;

                    candidateListModels.Candidates = from n in candidateListModels.Candidates.ToList().GetRange((page - 1) * ItemsOnPage, count)
                                                     select n;
                }
                
                ViewBag.CurrentPage = page;

                #endregion
                
                ViewBag.CurrentKey = key;
                

                return View(candidateListModels);
            }
        }

       

        private object GetPositionsByKey(int p)
        {
            throw new NotImplementedException();
        }

    
        public ActionResult GetFile(int Id)
        {
            using (WebSiteEnteties websiteEntities = new WebSiteEnteties(entityConnection))
            {
                var candidateResume = (from n in websiteEntities.CandidateResumes
                                       where n.CandidateID == Id
                                       select n).FirstOrDefault();

                if (candidateResume != null)
                {
                    if (candidateResume.Resume != null)
                    {
                        Response.ContentType = "application/msword";

                        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", candidateResume.ResumeName));

                        Response.AddFileDependency(candidateResume.ResumeName);

                        Response.BinaryWrite(candidateResume.Resume);
                    }
                }

                return null;
            }
        }
    }
}
